<?php
include_once "function_xml_basic.php";

$xpath_root = "/MINI/NETWORK/INTERFACES";
$xpath_node = "/INTERFACE";

function GetMaxDevNumberForPPPOE()
{
	$ret = 2;
	return $ret;
}

function GetAttrListForPPPOE()
{
	$returns = array("Name", "Type", "Enabled", "Level", "ETH", "USER", "PASS", "DEMAND", "DNSTYPE", "DNS1", "DNS2", "DEFAULTROUTE", "PIDFILE");
	return $returns;
}

function GetNodeNameFromXpath($xpath_node)
{
	$node_name = substr($xpath_node,1);
	return $node_name;
}

function GetXmlPPPOEDevList()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[@Type='pppoe']";
	$key_array = GetAttrListForPPPOE();
	
	$list = GetAttributList($query_string, $key_array);
	#for ($i = 0; $i < count($list); $i ++)
	#{
	#	foreach ( $key_array as $key)
	#		print $i. $key. " = ". $list[$i][$key]. "\n";
	#}
	return $list;
}

function GetXmlPPPOEDevCount()
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[@Type='pppoe']";
	$ret = GetNumberOfNode($query_string);
	return $ret;
}
# $ret = GetXmlPPPOEDevCount();
# print $ret;

function GetXmlPPPOEDevLevel()
{
	global $xpath_root;
	global $xpath_node;
	$query_string = $xpath_root. $xpath_node;

        $ret_list1 = GetAllNodeValue($query_string. "[@Type='pppoe' and @Level='1']");
	//print_r ($ret_list1);
	foreach ($ret_list1 as $r1){
		return 1;
	}

        $ret_list2 = GetAllNodeValue($query_string. "[@Type='pppoe' and @Level='2']");
	//print_r ($ret_list2);
	foreach ($ret_list2 as $r2){
		return 2;
	}
	return 0;
}

function GetXmlPPPOEDevByName($name)
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node;

        $ret_list = GetAllNodeValue($query_string. "[@Name='". $name. "' and @Type='pppoe']");
	$result = $ret_list -> item(0);
	
	$key_array = GetAttrListForPPPOE();
	foreach ( $key_array as $key)
	{
		$ret_node[$key] = $result->getAttribute($key);
		#print $key. " -> ". $ret_node[$key]. "\n";
	}
	return $ret_node;
}
#$node = GetXmlPPPOEDevByName("pppoe0");

function GetNetDevPPPOEInfoByEth($eth)
{
	$dev_list =  GetXmlPPPOEDevList();

	foreach ($dev_list as $p){
		if ($p["ETH"] == $eth)
			return $p;
	}
	return FALSE;
}

function AppendXmlPPPOEDev($in_array)
{
	global $xpath_root;
	global $xpath_node;
	
	$node_name = GetNodeNameFromXpath($xpath_node);
	$in_array['Type'] = 'pppoe';
	$query_string = $xpath_root;
	//CheckNodeExist($query_string);
	AppendAllAttrOfNode($query_string, $node_name, $in_array);
}
#$list["Name"] = "pppoe2";
#$list["Type"] = "PPPOE";
#$list["ETH"] = "eth0";
#AppendXmlPPPOEDev($list);

function SetXmlPPPOEDevByName($name, $in_array )
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[@Name='". $name. "' and @Type='pppoe']";
	EditAllAttrOfNode($query_string, $in_array);
}

function DelXmlSpecialOnePPPOEByName($name)
{
	global $xpath_root;
	global $xpath_node;

	$query_string = $xpath_root. $xpath_node. "[@Name='". $name. "' and @Type='pppoe']" ;

	DelSpecialNode($query_string);
}
#DelXmlSpecialOnePPPOEByName("pppoe1");

function DelXmlAllPPPOEDev()
{
	global $xpath_root;
	global $xpath_node;
	$query_string = $xpath_root. $xpath_node. "[@Type='pppoe']";

	DelSpecialNode($query_string);
}

function ApplyXmlPPPOEDev()
{
	$CP = "/bin/cp ";
	$SED = "/bin/sed ";
	$ECHO = "/bin/echo ";
	$EGREP = "/bin/egrep ";
	$PPPOE_START = "/usr/sbin/adsl-start ";
	$command = "";
	$start_command = "";
	$pid_path_root = "/var/run/pid.pppoe";
	$config_path_root = "/etc/ppp/pppoe.conf";
	$i = 0;
	$p_list = GetXmlPPPOEDevList();
	foreach ($p_list as $p){
		$config_path = $config_path_root. $i;
		$pid_path = $pid_path_root. $i;
		$user = $p['USER'];
		$pass = $p['PASSWORD'];
		$eth = $p['ETH'];

		$command  = $SED ;
		$command .= ' -e "s&^USER=.*&USER=\''. $user. '\'&" ';
		$command .= ' -e "s&^ETH=.*&ETH=\''. $eth. '\'&" ';
		$command .= ' -e "s&^PIDFILE=.*&PIDFILE=\\"'. $pid_path. '\\"&" < '. $config_path_root. ' > '. $config_path;
		#print $command. "\n";
		$ret = shell_exec($command);
		$ready_command = $CP. ' -rf /etc/ppp/pap-secrets /etc/ppp/pap-secrets-bak;  '.
    				 $CP. ' -rf /etc/ppp/chap-secrets /etc/ppp/chap-secrets-bak ';
		#print $ready_command. "\n";
		$ret = shell_exec($ready_command);
	
		$command = $EGREP. ' -v "^'. $user. '|^\"'. $user. '\"" /etc/ppp/pap-secrets-bak >/etc/ppp/pap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		$command = $ECHO. ' "\"'. $user. '\"	*	\"'. $pass. '\"" >> /etc/ppp/pap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		
		$command = $EGREP. ' -v "^'. $user. '|^\"'. $user. '\"" /etc/ppp/chap-secrets-bak >/etc/ppp/chap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		$command = $ECHO. ' "\"'. $user. '\"	*	\"'. $pass. '\"" >> /etc/ppp/chap-secrets';
		#print $command. "\n";
		$ret = shell_exec($command);
		
		$PPPOE_START = "/usr/sbin/adsl-start ";
		$start_command = $PPPOE_START . $config_path;
		$ret = shell_exec($start_command);

		$i++;
	}

	return $ret;
}


?>
